﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

public class EveryDayGift : MonoBehaviour {
    public GameObject GiftPanel;
    public GameObject DayItem;
    private string timer;
	// Use this for initialization
	void Start () {
        InitDayItem();
        UpdateDayItem();
        timer = getUnix().ToString();
        SetTime( timer );
        Debug.Log( "Time:"+timer );
        
    }
	
	// Update is called once per frame
	void Update () {

        UpdateDayItem();
    }
    public void SetTime(string time)
    {
        if( PlayerPrefs.GetString( "startTime", "0" ) == "0" )
        {
            PlayerPrefs.SetString( "startTime", time );
            for( int i = 0; i < 9; i++ )
            {
                string num = i.ToString();
                PlayerPrefs.SetInt( "day" + num, 0 );
            }
        }
    }
    public static long getUnix()
    {
        try
        {
            //TimeSpan timespan = DateTime.UtcNow - new DateTime( 1970, 1, 1 );            
            //return (long)timespan.TotalSeconds;//获取10位
            DateTime dt = new DateTime(1970, 1, 1);
            DateTime.Now.ToString("yyyy-MM-dd");
            DateTime tim = DateTime.Now.Date;
            TimeSpan d =tim - dt;
            long seconddiff = d.Ticks / 10000000;
            return seconddiff;
           // return (long)timespan.TotalMilliseconds;
        }
        catch( Exception )
        {
            return -1;
        }
    }

    public void InitDayItem()
    {
        foreach( Transform child in GiftPanel.transform )
        {
            Destroy( child.gameObject );
        }
        for( int i = 0; i < 9; i++ )
        {
            GameObject go = Instantiate( DayItem );
            int x = i % 3;
            int y = i / 3;
            go.transform.parent = GiftPanel.transform;
            go.name = "dayItem" + i;
            go.transform.localPosition = new Vector3( -170 + x * 170, 280 - y * 210 );
            go.transform.localScale = new Vector3( 1, 1 );
            go.gameObject.GetComponent<UISprite>().spriteName = "day" + i;
            go.transform.Find( "xbg/Label" ).GetComponent<UILabel>().text = (5 + i * 5).ToString();
            string day = (i + 1).ToString();
            go.transform.Find( "dayLabel" ).GetComponent<UILabel>().text = "第" + day + "天";
        }
    }

    public void UpdateDayItem()
    {
        for( int i = 0; i < 9; i++ )
        {
            string path = "dayItem" + i.ToString()+"/dayLabel";
            if( PlayerPrefs.GetInt( "day"+i.ToString() ) == 1 )
            {
                GiftPanel.transform.Find( path ).GetComponent<UILabel>().text = "已领取";
            }
            else
            {
                string day = ( i + 1 ).ToString();
                GiftPanel.transform.Find( path ).GetComponent<UILabel>().text = "第"+day+"天";
            }
        }
    }
    public void LiQuClick()
    {
        TimeSpan timespan = DateTime.UtcNow - new DateTime( 1970, 1, 1 );            
        long nowTime=(long)timespan.TotalSeconds;//获取10位
        for (int i = 0; i < 9; i++)
        {
            if (PlayerPrefs.GetInt("day" + i.ToString()) == 0)
            {
           
                string startTime = PlayerPrefs.GetString("startTime");
                if (long.Parse(startTime) + i * 86400 <= nowTime && nowTime < long.Parse(startTime)+(i+1)*86400)
                {
                    string path = "dayItem" + i.ToString() + "/dayLabel";
                    PlayerPrefs.SetInt("day" + i.ToString(), 1);
                    GiftPanel.transform.Find(path).GetComponent<UILabel>().text = "已领取";
                    PlayerPrefs.SetInt("coin", PlayerPrefs.GetInt("coin",0) + 5+i*5);
                    AudioManager.instance.AddCoinOnclick();
                }
            }
            else
            {
                return;
            }
        }
    }
}
